using System;
using Game.Framework.Utils;

namespace Game.Framework.FState
{
    public class StateMachine<TState> where TState : BaseState
    {
        private TState _currentState;
        public TState CurrentState => _currentState;
        public Type LasStateType;

        public void SetState<U>(StateData param) where U : TState, new()
        {
            if (_currentState != null)
            {
                LasStateType = _currentState.GetType();

                (_currentState as State<TState>)?.Exit();
            }

            _currentState = new U();
            var t = _currentState as State<TState>;
            if (t != null)
            {
                t.SetStateMachine(this);
                t.Enter(param);
            }
            else
            {
                LogUtil.LogError("t is null");
            }
        }

        public virtual void Update()
        {
            (_currentState as State<TState>)?.Update();
        }

        public virtual void FixedUpdate(float deltaTime)
        {
            (_currentState as State<TState>)?.FixedUpdate(deltaTime);
        }

        public virtual void Release()
        {
            (_currentState as State<TState>)?.Exit();
        }
    }
}